//
//  MessagesController.m
//  Job
//
//  Created by 欧松 on 2018/8/25.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "MessagesController.h"
#import "MessageCell.h"
#import "XLMsgModel.h"
#import "LoginViewController.h"

@interface MessagesController ()
@property(nonatomic,strong)XLAuthModel *auth;
@end

@implementation MessagesController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:@"MessageCell"];
    [self.tableView.mj_header beginRefreshing];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.auth = [XLAuthModel getAuth];
}
- (void)down_refreshData {
    self.page = 0;
    [self getMessages:YES];
}

-(void)loadMoreData{

    self.page ++;
    [self getMessages:NO];
}
- (void)getMessages:(Boolean)isRefresh{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"page_size"] = @"10";
    dict[@"start_index"] = @(self.page);
    dict[@"user_id"] = self.auth.userID;
    [HTTPTools GET:getMessagesUrl parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (!error) {
            if (!msg) {
                NSArray *array = [XLMsgModel mj_objectArrayWithKeyValuesArray:result];
                if (isRefresh) {
                    [self.dataSource removeAllObjects];
                }
                [self.dataSource addObjectsFromArray:array];
                for (XLMsgModel *model in self.dataSource) {
                    if (model.jobId == 0) {
                        [self.dataSource removeObject:model];
                    }
                }
                [self reloadData];
            }
        }
    }];
}
#pragma mark - UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120.0;
}










@end

